package com.themedemo;

import org.apkplug.Bundle.OnThemeChengeListener;
import org.apkplug.Bundle.RegThemeChengeListener;
import org.apkplug.Bundle.ThemeControl;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;

import com.apkplug.Service.ThemeCheng;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ServiceRegistration m_reg = null;
	private ServiceReference ref=null;
	private BundleContext context=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.out.println("reCreate");
		//this.obtainStyledAttributes(R.style.AppTheme_Default, com.android.internal.R.styleable.Window);
		setContentView(R.layout.activity_main);
		context=BundleContextFactory.getInstance().getBundleContext();
		ListenerTheme();
		final Button button1=(Button)this.findViewById(R.id.button1);
		button1.setOnClickListener(
				new OnClickListener(){
					public void onClick(View v) {
						// TODO Auto-generated method stub
						try {
							System.out.println("注册主题");
							setTheme1(R.style.AppTheme_Default,true);
							
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
		});
		final Button button2=(Button)this.findViewById(R.id.button2);
		button2.setOnClickListener(
				new OnClickListener(){
					public void onClick(View v) {
						// TODO Auto-generated method stub
						try {
							setThemeToSystem();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
		});
		final Button button3=(Button)this.findViewById(R.id.button3);
		button3.setOnClickListener(
				new OnClickListener(){
					public void onClick(View v) {
						// TODO Auto-generated method stub
						try {
							setTheme1(R.style.AppTheme_Another,false);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
		});
		final Button Main_Bundle_Activity=(Button)this.findViewById(R.id.Main_Bundle_Activity);
		Main_Bundle_Activity.setOnClickListener(
				new OnClickListener(){
					public void onClick(View v) {
						Intent i=new Intent();
		    			i.setClass(MainActivity.this,Main_Bundle_Activity.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						MainActivity.this.startActivity(i);
					}
		});
		final Button bundle_Activity=(Button)this.findViewById(R.id.Bundle_Activity);
		bundle_Activity.setOnClickListener(
				new OnClickListener(){
					public void onClick(View v) {
						Intent i=new Intent();
		    			i.setClass(MainActivity.this,Bundle_Activity.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						MainActivity.this.startActivity(i);
					}
		});
		
		final Button bundle_HideActivity=(Button)this.findViewById(R.id.Bundle_HideActivity);
		bundle_HideActivity.setOnClickListener(
				new OnClickListener(){
					public void onClick(View v) {
						Intent i=new Intent();
		    			i.setClass(MainActivity.this,Bundle_HideActivity.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						MainActivity.this.startActivity(i);
					}
		});
		
		final Button main_Bundle_Hide=(Button)this.findViewById(R.id.Main_Bundle_Hide);
		main_Bundle_Hide.setOnClickListener(
				new OnClickListener(){
					public void onClick(View v) {
						Intent i=new Intent();
		    			i.setClass(MainActivity.this,Main_Bundle_Hide.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						MainActivity.this.startActivity(i);
					}
		});
	
		if(ThemeChengFactory.service!=null)
			main_Bundle_Hide.setBackgroundResource(ThemeChengFactory.service.buttonStyle());
	}
	public void ListenerTheme(){
		
		ServiceReference reference=context.getServiceReference(RegThemeChengeListener.class.getName());
    	if(null!=reference){
    		final RegThemeChengeListener service=(RegThemeChengeListener) context.getService(reference);
    		if(service!=null){
    			//插件启动级别为1(会自启) 并且不检查插件版本是否相同都安装
    			service.registerOnThemeListener(
    					new OnThemeChengeListener(){
							@SuppressLint("NewApi")
							@Override
							public void afterChenged(
									org.osgi.framework.Bundle arg0, int arg1) {
								MainActivity.this.recreate();
								System.out.println("afterChenged");
								service.unregisterOnThemeListener(this);
							}

							@Override
							public void beforeChenge(
									org.osgi.framework.Bundle bb, int arg1,org.osgi.framework.Bundle tob, int arg2) {
								
							}
    			});
    		}
    		context.ungetService(reference);
    	}
    	
    	try {
    		context.addServiceListener(
					new ServiceListener(){
						@Override
						public void serviceChanged(ServiceEvent event) {
							switch  (event.getType()) {
								case  ServiceEvent.REGISTERED:
									if  (ref  ==   null ) {
										System.out.println("主题切换服务");
										ref  =  event.getServiceReference();
										ThemeChengFactory.service  =  (ThemeCheng) context.getService(ref);//保存实例以备后续访问
									}
									break ;
								case  ServiceEvent.UNREGISTERING:
									if  (ref  ==  event.getServiceReference()) {
										context.ungetService(ref); //释放实例 
										ThemeChengFactory.service  =   null ;
											ref  =   null ;
									}
									break ;
								}
							
						}
			},"(objectclass=com.apkplug.Service.ThemeCheng)" );
		} catch (InvalidSyntaxException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	public void setTheme1(int themeid,boolean t) throws Exception{
		BundleContext mcontext=BundleContextFactory.getInstance().getBundleContext();
		ServiceReference reference=mcontext.getServiceReference(ThemeControl.class.getName());
    	if(null!=reference){
    		ThemeControl service=(ThemeControl) mcontext.getService(reference);
    		if(service!=null){
    			if(t){
	    			service.addStyles(mcontext, ThemeCheng.class.getName(), 
	    			new ThemeCheng(){
						@Override
						public int buttonStyle() {
							return R.drawable.buttonstyle;
						}	
					});
    			}else{
    				service.removeStyles(mcontext, ThemeCheng.class.getName());
    			}
    			service.setTheme(mcontext, mcontext.getBundle(),themeid);
    			
    		}
    	mcontext.ungetService(reference);
    	}
	}	
	public void setThemeToSystem() throws Exception{
		BundleContext mcontext=BundleContextFactory.getInstance().getBundleContext();
		ServiceReference reference=mcontext.getServiceReference(ThemeControl.class.getName());
    	if(null!=reference){
    		ThemeControl service=(ThemeControl) mcontext.getService(reference);
    		if(service!=null){
    			service.setTheme(mcontext,0);
    		}
    	mcontext.ungetService(reference);
    	}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
